<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>车间管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__UI__/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="__UI__/css/public.css" media="all">
    <link rel="stylesheet" href="__UI__/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">设备名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="dev_name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">生产商</label>
                            <div class="layui-input-inline">
                                <input type="text" name="dev_manufacturer" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">设备状态</label>
                            <div class="layui-input-inline">
                                <select name="dev_status">
                                    <option value="" selected>不限</option>
                                    <option value="正在使用">正在使用</option>
                                    <option value="已关机">已关机</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">

            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>

        </script>

    </div>
</div>
<script src="__UI__/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="__UI__/js/common.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '{:url("admin/spdevice/index")}',
            method: 'post', //提交方式
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: '编号', sort: true},
                {field: 'dev_name', width: 150, title: '设备名称'},
                {field: 'dev_date', width: 110, title: '生产日期'},
                {field: 'dev_manufacturer', width: 250, title: '生产商'},
                {field: 'dev_pic', width: 150, title: '设备照片',templet:function (d) {
                        if (d.dev_pic!==""){
                            return '<img src="/'+ d.dev_pic +'">';
                        }
                        return "";
                    }},
                {field: 'dev_price', width: 100, title: '设备价格'},
                {field: 'dev_power', width: 100, title: '设备功率'},
                {field: 'dev_type', width: 100, title: '设备类型'},
                {field: 'dev_status', width: 100, title: '设备状态'},
                {field: 'remark', width: 180, title: '备注'},

                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "left"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            sort: 'id',
            limit: 10, //每页显示的行数
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载(reload:重新加载)
            table.reload('currentTableId', {
                page: {
                    curr: 1 //回到第一页
                }
                , where: {
                    searchParams: data.field
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '新增设备信息',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '{:url("admin/spdevice/create")}',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                //判断用户是否选中数据
                if(data.length === 0){
                    layer.msg('请勾选要删除的数据！',{icon:5});
                    return false;
                }
                layer.confirm('确定要删除选中数据吗？', function (index) {
                    var ids = [];
                    $.each(data,function (k,v) {
                        ids[k] = v.id;
                    });
                    postSubmit(
                        $,
                        "{:url('admin/spdevice/delete')}",
                        {id:ids},
                        function (res) {
                            //请求错误时的处理,currentTableId:是表格的id
                            table.reload('currentTableId');//刷新表格的数据
                        },
                        function () {} //请求错误时的处理
                    );
                    layer.close(index);
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            console.log(data.id);
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '编辑设备信息',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '{:url("admin/spdevice/edit")}?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定要删除这行数据么?', function (index) {
                    postSubmit(
                        $,
                        "{:url('admin/spdevice/delete')}",
                        {id:obj.data.id},
                        function (res) {
                            //请求错误时的处理,currentTableId:是表格的id
                            table.reload('currentTableId');//刷新表格的数据
                        },
                        function () {} //请求错误时的处理
                    );
                    layer.close(index);
                });
            }
        });

    });
</script>

</body>
</html>